package com.yyplatform.admin.platform.config.controller;

import static com.yyplatform.admin.Constants.URL_PREFIX;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyplatform.admin.infra.model.RestResponse;
import com.yyplatform.admin.platform.config.model.dto.SysDictItemDTO;
import com.yyplatform.admin.platform.config.model.dto.SysDictItemFormDTO;
import com.yyplatform.admin.platform.config.model.dto.SysDictItemQueryDTO;
import com.yyplatform.admin.platform.config.service.SysDictItemService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-13
 */
@Slf4j
@Tag(name = "配置/字典项管理")
@AllArgsConstructor
@RestController
@RequestMapping(URL_PREFIX + "/sys/dict/item/")
public class SysDictItemController {

    private SysDictItemService sysDictService;


    @GetMapping("query")
    @Operation(summary = "查询")
    @RequiresPermissions("sys:dictItem:query")
    public RestResponse<Page<SysDictItemDTO>> query(SysDictItemQueryDTO queryDTO) {
        log.debug("query dto = {}", queryDTO);

        Page<SysDictItemDTO> page = sysDictService.query(queryDTO);

        return RestResponse.ok(page);
    }


    @GetMapping("get")
    @Operation(summary = "通过ID获取详情")
    @RequiresPermissions("sys:dictItem:get")
    public RestResponse<SysDictItemDTO> get(Long id) {
        log.debug("get by id is {}", id);

        SysDictItemDTO data = sysDictService.get(id);
        return RestResponse.ok(data);
    }


    @PostMapping("save")
    @Operation(summary = "保存")
    @RequiresPermissions("sys:dictItem:save")
    public RestResponse<Void> save(@RequestBody SysDictItemFormDTO sysDictFormDTO) {
        log.debug("save dto is {}", sysDictFormDTO);

        sysDictService.save(sysDictFormDTO);

        return RestResponse.ok(null);
    }


    @PostMapping("update")
    @Operation(summary = "修改")
    @RequiresPermissions("sys:dictItem:update")
    public RestResponse<Void> update(@RequestBody SysDictItemFormDTO formDTO) {
        log.debug("update dto is {}", formDTO);

        sysDictService.update(formDTO);
        return RestResponse.ok(null);
    }


    @PostMapping("delete")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:dictItem:delete")
    public RestResponse<Void> delete(Long id) {
        log.debug("delete by id is {}", id);

        sysDictService.delete(id);
        return RestResponse.ok(null);
    }


}
